package com.miet.mp45.filippov.cryptoapplet.actionpanels;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;

import javax.swing.JFileChooser;

import org.apache.log4j.Logger;

import com.miet.mp45.filippov.crypto.util.CryptoUtil;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingFinishedEventListener;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingStartedEventListener;


public class KeyGenPanel extends ActionPanel
{

    private static final long serialVersionUID = 7622510367211679671L;
    
    private Logger logger = Logger.getLogger(this.getClass());
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField keySizeTextField;
    
    public KeyGenPanel(ProcessingStartedEventListener pseListener,
                       ProcessingFinishedEventListener pfeListener)
    {
        addPSEListener(pseListener);
        addPFEListener(pfeListener);
        initComponents();
    }

    private void initComponents()
    {

        jLabel1 = new javax.swing.JLabel();
        keySizeTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Size:");

        keySizeTextField.setText("2048");

        jLabel2.setText("bit");

        jButton1.setText("Generate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(keySizeTextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(keySizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        FILE_CHOOSER.setFileFilter(PRIVATE_KEY_FILE_FILTER);
        if (FILE_CHOOSER.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        {
//            setVisible(false);
            new CryptoTask()
            {
                @Override
                public void doRun()
                {
                    FileOutputStream fos = null;
                    try
                    {
                        File file = FILE_CHOOSER.getSelectedFile();
                        logger.info("Generating key pair ...");
                        KeyPair keyPair = CryptoUtil.generateKeyPair(Integer.parseInt(keySizeTextField.getText()));
                        logger.info("Writing key pair into '" + file + "'");
                        fos = new FileOutputStream(file);
                        fos.write(keyPair.getPrivate().getEncoded());
                    }
                    catch (Exception ex)
                    {
                        logger.error(ex);
                    }
                    finally
                    {
                        if (fos != null)
                        {
                            try { fos.close(); } catch (IOException ex) { logger.error(ex); }
                        }
                    }
                }
            }.start();
        }    
    }
    
}
